package com.imessage.action.base.product;

import com.bean.BaseProduct;
import com.imessage.comm.Constants;
import com.imessage.vo.ProductVo;
import com.imessage.web.HttpJson;
import com.imessage.web.Page;
import io.ebean.Expression;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/admin/product")
public class ProductAction extends ProductBaseAction {

    @RequestMapping(value = "")
    public Object run(ProductVo productVo) {
        List<Expression> expressionList = new ArrayList<Expression>();
        setExpressionList(expressionList, productVo);
        Page<BaseProduct> page = baseService.findPage(BaseProduct.class, expressionList,
                null, getOrder(productVo.getOrders()),
                productVo.getPageCurrent(), productVo.getPageSize());
        return page;
    }

    @RequestMapping(value = "initpage")
    public ModelAndView initpage(
            @RequestParam(name = "req_id", required = true) String id) {
        ModelAndView modelAndView = new ModelAndView();
        BaseProduct baseProduct = new BaseProduct();
        if (StringUtils.isNotBlank(id)) {
            baseProduct = baseService.findById(BaseProduct.class, id);
            modelAndView.setViewName("admin/base/product/modify");
        } else {
            modelAndView.setViewName("admin/base/product/add");
        }
        setModelAndView(modelAndView, baseProduct);
        return modelAndView;
    }

    @RequestMapping(value = "add")
    public Object add(ProductVo productVo) {
        BaseProduct baseProduct = new BaseProduct();
        setBaseProduct(productVo, baseProduct);
        int state = baseService.saveObject(baseProduct);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }

    @RequestMapping(value = "modify")
    public Object modify(ProductVo productVo) {
        HttpJson json = null;
        BaseProduct baseProduct = baseService.findById(BaseProduct.class, productVo.getId());
        setBaseProduct(productVo, baseProduct);
        int state = baseService.updateObject(baseProduct);
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return baseProduct;
        }
        return getJsonForErr("", datagrid);
    }

    @RequestMapping(value = "del")
    public Object del(@RequestParam(name = "id", required = true) String ids) {
        int state = Constants.STATE_OPERATOR_LOST;
        for (String id : ids.split(",")) {
            BaseProduct baseProduct = baseService.findById(BaseProduct.class, id);
            state = baseService.delObject(baseProduct);
        }
        if (state == Constants.STATE_OPERATOR_SUCC) {
            return getJsonForSucc("", datagrid);
        }
        return getJsonForErr("", datagrid);
    }

    @RequestMapping(value = "/initimp")
    public ModelAndView initimp() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("action", "/admin/product/imp");
        modelAndView.addObject("templeturl","/static/file/product.xlsx");
        modelAndView.setViewName("admin/comm/imp");
        return modelAndView;
    }

    @RequestMapping(value = "/imp")
    public Object imp(MultipartFile upfile){
        String filePath= fileUpload(upfile);
        if(StringUtils.isBlank(filePath)){
            return getJsonForErr("上传文件为空或者格式不正确,只支持xls、xlsx格式", datagrid);
        }
        if(saveExcel(filePath)){
            return getJsonForSucc("上传成功", datagrid);
        }
        return getJsonForErr("上传失败", datagrid);
    }
}
